package com.intel.factorytest.ChamberMode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Looper;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.app.ActionBar;
import android.os.SystemProperties;

import com.intel.factorytest.R;
import com.intel.factorytest.util.LogUtils;
import com.intel.factorytest.util.Util;
import com.intel.factorytest.util.ShellUtilsPlus;
import com.intel.factorytest.ChamberMode.ChamberModeMainActivity;
import com.intel.factorytest.ChamberMode.ChambermodeBaseActivity;




public class BluetoothTest extends ChambermodeBaseActivity {
    
    private final static int BT_TEST_MSG_UPDATE_UI = 1;
    private final static int BT_TEST_MSG_TIME_OUT = 2;    
    private final static int BT_TEST_MSG_FTMDAEMON = 3;    
    private final static int BT_TEST_MSG_BT_DISABLE = 4;
    private final static int BT_TEST_MSG_BT_TX_START = 5;
    private final static int BT_TEST_MSG_BT_TX_STOP = 6;

    private static String BT_TX_TRAN_START_DIAG_CMD_FILE = "/vendor/bin/diagcmd/bt_tx.txt";    
    private static String BT_TX_TRAN_STOP_DIAG_CMD_FILE = "/vendor/bin/diagcmd/bt_tx_stop.txt";
    private static String BT_FTM_CMD = "ftmdaemon -n -dd";    
    private static String BT_DISABLE_CMD = "svc bluetooth disable";

    private int mTimeout = 20000;    
    //private BtTestDown mBtTestDown = null;
    ShellUtilsPlus.CommandResult mCmdResult;
    private String cmdSuccessMsg;
    private String cmdErrorMsg;
    private boolean isBTTransmiting = false;

    
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case BT_TEST_MSG_UPDATE_UI: {
                    removeMessages(BT_TEST_MSG_UPDATE_UI);
                    updateTitle("");
                    break;
                }
                case BT_TEST_MSG_TIME_OUT: {
                    removeMessages(BT_TEST_MSG_TIME_OUT);                   
                    Util.isBluetoothTesting = false ;                     
                    SystemProperties.set("persist.sys.chamber_run","true");
                    finish();
                    break;
                }
                case BT_TEST_MSG_BT_DISABLE: {
                    removeMessages(BT_TEST_MSG_BT_DISABLE);                  
                    BTDisableExec();                     
                    sendDelayedMsg(BT_TEST_MSG_FTMDAEMON,2000,null,null,mHandler); 
                    break;
                }
                case BT_TEST_MSG_FTMDAEMON: {
                    removeMessages(BT_TEST_MSG_FTMDAEMON);                  
                    BTFtmdaemonExec();                    
                    sendDelayedMsg(BT_TEST_MSG_BT_TX_START,1000,null,null,mHandler); 
                    break;
                }
                case BT_TEST_MSG_BT_TX_START: {
                    removeMessages(BT_TEST_MSG_BT_TX_START);
                    btTransmitStart();                    
                    updateTitle("BT");
                    sendDelayedMsg(BT_TEST_MSG_UPDATE_UI,5000,null,null,mHandler); 
                    isBTTransmiting = true;
                    sendDelayedMsg(BT_TEST_MSG_BT_TX_STOP,20000,null,null,mHandler); 
                    break;
                }
                case BT_TEST_MSG_BT_TX_STOP: {
                    removeMessages(BT_TEST_MSG_BT_TX_STOP);
                    btTransmitStop();                    
                    isBTTransmiting = false;
                    sendDelayedMsg(BT_TEST_MSG_TIME_OUT,1000,null,null,mHandler); 
                    break;
                }
                
                default:
                    break;
            }
        }
    };

    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        LogUtils.loge("onCreate");
    }

    @Override
    public void onResume() {
        LogUtils.loge("onResume");
        super.onResume();        
        SystemProperties.set("persist.sys.chamber_run","false");
        //SystemProperties.set("persist.sys.chamber.bandtest","null");
        //SystemProperties.set("persist.sys.chamber.diagcmd","null");
    }
    
    @Override
    protected void startTest(){
        LogUtils.logi("startBTTest");
        Util.isBluetoothTesting = true ;       
        sendDelayedMsg(BT_TEST_MSG_BT_DISABLE,0,null,null,mHandler); 
        /*
        if (mTimeout > 0){
            LogUtils.logi("startBTTest 20s");
            new countDownThread().start();
        }
        */
    }
    
    @Override
    protected void stopTest(){
        LogUtils.logi("stopBTTest");
        if(isBTTransmiting) {
           sendDelayedMsg(BT_TEST_MSG_BT_TX_STOP,0,null,null,mHandler); 
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtils.loge( "onPause() ");
        //if ( mBtTestDown != null ) mBtTestDown.cancel();
    }

    @Override
    public void onStop() {
        super.onPause();
        LogUtils.loge( "onStop() ");
        if(isBTTransmiting) {
            btTransmitStop();
        }
        SystemProperties.set("persist.sys.chamber.bandtest","null");
        SystemProperties.set("persist.sys.chamber.diagcmd","null");
        /*
        if ( mBtTestDown != null ) mBtTestDown.cancel();
        if (mBtTestDown != null) {
            mBtTestDown = null;
        }
        */
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.loge( "onDestroy() ");        
        /*
        if (mBtTestDown != null) {
            mBtTestDown = null;
        }
        */
    }
    
    @Override
    public void finish() {
        Intent i = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString(ChamberModeMainActivity.BT_RESULT, "bt_test_finish");        
        i.putExtras(bundle);
        setResult(ChamberModeMainActivity.START_BT_TEST_RESULT_CODE,i);
        super.finish();
    }
/*
    private class countDownThread extends Thread{
        public countDownThread(){}

        @Override
        public void run() {
            LogUtils.logi("countDownThread  run()");
            // TODO Auto-generated method stub
            Looper.prepare();
            mBtTestDown = new BtTestDown();
            mBtTestDown.start();
            Looper.loop();
        }
    }

    private class BtTestDown extends CountDownTimer{

        public BtTestDown() {
            super(mTimeout,mTimeout);
        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            LogUtils.logi("Time Out:");
            mHandler.sendEmptyMessage(BT_TEST_MSG_TIME_OUT);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
        }
    }
*/
    private Thread mBtTestTxThread;

    public void btTxCmdExecThread(String path) {        
        LogUtils.logi( "btTxCmdExecThread path = " + path);
        mBtTestTxThread = new Thread(new BtTestTxCmdRunnable(path));
        mBtTestTxThread.start();
    }

    private void btTransmitStart(){
        LogUtils.logi("btTransmitStart");
        updateMessage("BT Tx Start");        
        SystemProperties.set("persist.sys.chamber.bandtest","bttx");
        //btTxCmdExecThread(BT_TX_TRAN_START_DIAG_CMD_FILE);
    }

    private void btTransmitStop(){
        LogUtils.logi("btTransmitStop");        
        updateMessage("BT Tx Stop");        
        SystemProperties.set("persist.sys.chamber.bandtest","btstop");
        //btTxCmdExecThread(BT_TX_TRAN_STOP_DIAG_CMD_FILE);
    }

    private class BtTestTxCmdRunnable implements Runnable{

        String btTxPath;
        public BtTestTxCmdRunnable(String btTxPath){
            this.btTxPath = btTxPath;
        }

        @Override
        public void run() {
            LogUtils.logi("BtTestTxCmdRunnable run() bandpath = " + btTxPath);
            mCmdResult = ShellUtilsPlus.execCommand("diag_tools -f" + btTxPath,false);
            cmdSuccessMsg = mCmdResult.successMsg;
            cmdErrorMsg = mCmdResult.errorMsg;
            LogUtils.logi( "BtTestTxCmdRunnable get Success Msg = " + cmdSuccessMsg + ", Error Msg = " + cmdErrorMsg);
        }
    }

    private Thread mBTTestFtmdaemonThread;

    public void BTFtmCmdExtcThread(String cmdpath) {        
        LogUtils.logi( "BTFtmCmdExtcThread cmdpath = " + cmdpath);
        mBTTestFtmdaemonThread = new Thread(new BtTestFtmCmdRunnable(cmdpath));
        mBTTestFtmdaemonThread.start();
    }

    private void BTDisableExec(){
        LogUtils.logi("BTDisableExec");
        updateMessage("svc bluetooth disable");        
        SystemProperties.set("persist.sys.chamber.diagcmd","btdisable");
        //BTFtmCmdExtcThread(BT_DISABLE_CMD);
    }

    private void BTFtmdaemonExec(){
        LogUtils.logi("BTFtmdaemonExec");
        updateMessage("ftmdaemon -n -dd");        
        SystemProperties.set("persist.sys.chamber.diagcmd","txftm");
        //BTFtmCmdExtcThread(BT_FTM_CMD);
    }

    private class BtTestFtmCmdRunnable implements Runnable{

        String CmdPath;
        public BtTestFtmCmdRunnable(String CmdPath){
            this.CmdPath = CmdPath;
        }

        @Override
        public void run() {
            LogUtils.logi("BtTestFtmCmdRunnable run() CmdPath = " + CmdPath);
            mCmdResult = ShellUtilsPlus.execCommand(CmdPath,false);
            cmdSuccessMsg = mCmdResult.successMsg;
            cmdErrorMsg = mCmdResult.errorMsg;
            LogUtils.logi( "BtTestFtmCmdRunnable get Success Msg = " + cmdSuccessMsg + ", Error Msg = " + cmdErrorMsg);
        }
    }

}

